package com.dsunsoft.rtu.service.impl;

import com.dsunsoft.rtu.common.util.CommonUtils;
import com.dsunsoft.rtu.common.util.SecurityUtils;
import com.dsunsoft.rtu.core.base.BaseServiceImpl;
import com.dsunsoft.rtu.dao.SysRoleMapper;
import com.dsunsoft.rtu.dto.SysRoleDto;
import com.dsunsoft.rtu.entity.SysRole;
import com.dsunsoft.rtu.entity.SysRoleMenu;
import com.dsunsoft.rtu.query.SysRoleQuery;
import com.dsunsoft.rtu.service.SysRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class SysRoleServiceImpl extends BaseServiceImpl<SysRoleMapper, SysRole, SysRoleQuery> implements SysRoleService {

    @Override
    public List<String> getMenuByRoleId(String roleId) {
        return mapper.selectMenuByRoleId(roleId);
    }

    @Transactional
    @Override
    public boolean saveRoleMenu(SysRoleDto sysRole) {
        // 先删除再新增
        int n = 0;
        Date now = new Date();
        String uid = SecurityUtils.getCurrentUserLogin();
        mapper.deleteRoleMenuByRoleId(sysRole.getRoleId());
        for (String menuId : sysRole.getMenuIds()) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setId(CommonUtils.getUuid());
            sysRoleMenu.setRoleId(sysRole.getRoleId());
            sysRoleMenu.setMenuId(menuId);
            sysRoleMenu.setCreatedBy(uid);
            sysRoleMenu.setCreatedDate(now);
            sysRoleMenu.setLastModifiedBy(uid);
            sysRoleMenu.setLastModifiedDate(now);
            n += mapper.insertRoleMenu(sysRoleMenu);
        }
        return n > 0;
    }
}
